
package com.hc.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.hc.dao.IArticleDao;
import com.hc.domain.Article;
import com.hc.util.LoggerUtil;
import com.hc.util.StringUtil;
@Repository
public class ArticleDaoImpl extends BaseDaoImpl<Article> implements IArticleDao {
	
	private String text = " a.id, a.subImg, a.keyWords, a.type, a.tag, a.content, a.title, a.subContent, a.subTitle,"
			+ " a.createDate, a.publishDate, a.status, a.publishUserId, a.editorId, a.viewCount ";

	@Override
	public void saveArticle(Article art) {
		super.save(art);
	}

	@Override
	public List<Article> findPageByType(int page, int count, String type) {
		String sql = "SELECT"+text+" FROM article a WHERE status!=99 ";
		if (count<=0){
			count = 10;
		}
		if (page<=0){
			page = 1;
		}
		if (!StringUtil.isEmpty(type)){
			sql = sql + "AND a.type=? ";
		}
		int begin = (page-1)*count;
		sql = sql + " ORDER BY a.createDate DESC LIMIT "+ begin +","+count;
		try {
			List<Article> arts = super.executeSqlAuto(Article.class, sql, new Object[] {type});
			return arts;
		} catch (Exception e) {
			LoggerUtil.error(e);
		}
		return null;
	}

	@Override
	public Article getById(int id) {
		return super.get("FROM Article a WHERE a.id=?", new Object[] {id});
		
	}

	@Override
	public void updateArt(Article art) {
		super.update(art);
	}

	@Override
	public List<Article> findPageByLikeSql(int page, int count, String type, String words) {
		String sql = "SELECT"+text+" FROM article a WHERE status!=99 ";
		if (count<=0){
			count = 10;
		}
		if (page<=0){
			page = 1;
		}
		if (!StringUtil.isEmpty(type)){
			sql = sql + " AND a.type = :type ";
		}
		if (!StringUtil.isEmpty(words)) {
			sql = sql + " AND (a.keyWords Like :name OR a.tag Like :name OR a.title LIKE :name)";
		}
		
		int begin = (page-1)*count;
		sql = sql + " ORDER BY a.createDate DESC LIMIT "+ begin +","+count;
		try {
			return super.executeLikeSql(Article.class, sql, type,words);
		} catch (Exception e) {
			LoggerUtil.error(e);
		}
		return null;
	}

}

